package com.jt.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.annotation.CacheFind;
import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.CatNode;
import com.jt.pojo.CatResult;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import com.jt.pojo.SearchItem;
import com.jt.pojo.SearchResult;
import com.jt.service.ItemCatService;
import com.jt.utils.Assert;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dc
 * @Classname ItemCatServiceImpl
 * @Date 2020/2/7 17:02
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Override
    //@CacheFind
    public String findItemCatById(Integer itemCatId) {
        Assert.isValid(itemCatId<1,"商品分类出错");
        return itemCatMapper.selectById(itemCatId).getName();
    }

    /**
     * 根据parentId查询分类信息
     * @param parentId 父级id
     * @return 目录集合
     */
    private List<ItemCat> findItemCatList(Long parentId){
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        return itemCatMapper.selectList(queryWrapper);
    }

    @Override
    @CacheFind
    public List<EasyUITree> findItemCatByParentId(Long parentId) {
        List<ItemCat> itemCats = findItemCatList(parentId);
        List<EasyUITree> treeList = new ArrayList<>(itemCats.size());
        for(ItemCat itemCat:itemCats){
            Long id = itemCat.getId();
            String text = itemCat.getName();
            //如果是父级关闭,否则开启(父级显示为可打开文件夹,否则显示为文件图案且不可展开节点)
            String state=itemCat.getIsParent()?"closed":"open";
            EasyUITree tree=new EasyUITree(id,text,state);
            treeList.add(tree);
        }
        return treeList;
    }

    @Override
    public CatResult findItemCatList() {
        CatResult catResult =new CatResult();
        catResult.setData(getCatList(0));
        return catResult;
    }

    private List<?>getCatList(long parentId){
        //定义返回值列表
        List rs=new ArrayList<>();
        //查询分类列表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",parentId);
        List<ItemCat> itemCatList = itemCatMapper.selectList(queryWrapper);
        //向list中添加节点
        int count=0;
        for(ItemCat itemCat:itemCatList){
            //判断是否为父节点
            if(itemCat.getIsParent()){
                CatNode catNode=new CatNode();
                if(parentId==0){
                    catNode.setName("<a href='/products/"+itemCat.getId()+".html'>"+itemCat.getName()+"</a>");
                }else{
                    catNode.setName(itemCat.getName());
                }
                catNode.setUrl("/products/"+itemCat.getId());
                //递归调用,获取通过子节点进行查询
                catNode.setItem(getCatList(itemCat.getId()));
                rs.add(catNode);
                count++;
                //第一层第一级只取14条记录
                if(parentId==0&&count>=14){
                    break;
                }
            }else {
                //如果是叶子节点是,直接添加一个字符串
                rs.add("/products/"+itemCat.getId()+"|"+itemCat.getName());
            }
        }
        return rs;
    }

    /*@CacheFind*/
    @Override
    public SearchResult findItemsByCatId(Long itemCatId, Integer page, Integer rows) {
        SearchResult searchResult=new SearchResult();
        List<SearchItem> searchItems = new ArrayList<>();
        List<Long> itemCatIdList = new ArrayList<>();
        //获取分类id集合
        itemCatIdList = getItemCatId(itemCatId,itemCatIdList);
        IPage<Item> iPage = new Page<>(page, rows);
        QueryWrapper<Item> itemWrapper = new QueryWrapper<>();
        itemWrapper.in("cid",itemCatIdList);
        IPage<Item> resultPage = itemMapper.selectPage(iPage,itemWrapper);
        List<Item> itemList = resultPage.getRecords();
        for(Item item:itemList){
            SearchItem searchItem = new SearchItem();
            searchItem.setId(String.valueOf(item.getId()));
            searchItem.setImage(item.getImage());
            searchItem.setPrice(item.getPrice());
            searchItem.setSellPoint(item.getSellPoint());
            searchItem.setTitle(item.getTitle());
            searchItems.add(searchItem);
        }
        searchResult.setItemList(searchItems);
        searchResult.setCurrentPage(page);
        searchResult.setRecordCount(iPage.getTotal());
        searchResult.setPageCount(iPage.getPages());
        return searchResult;
    }


    private List<Long> getItemCatId(Long itemCatId,List<Long> itemCatIdList) {
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",itemCatId);
        ItemCat itemCat = itemCatMapper.selectOne(queryWrapper);
        if(itemCat.getIsParent()){
            QueryWrapper sonQuery = new QueryWrapper();
            sonQuery.eq("parent_id",itemCatId);
            List<ItemCat> sonList = itemCatMapper.selectList(sonQuery);
            for(ItemCat cat:sonList){
                getItemCatId(cat.getId(),itemCatIdList);
            }
        }else {
            itemCatIdList.add(itemCat.getId());
        }
        return itemCatIdList;
    }




}
